<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\common\model\platform;

use app\common\model\addon\Addons;
use app\common\model\BaseModel;
use app\common\model\user\User;
use app\common\model\user\UserPermission;
use app\common\model\wechat\WechatUser;
use think\model\concern\SoftDelete;

/**
 * 平台模型
 * @package app\common\model
 * User: liuhongbin
 * Date: 2021-05-25 14:14
 */
class Platform extends BaseModel
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'plaid';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'platform';

    public $_type = array(
        1 => "微信公众平台",
        2 => "小程序合集",
        3 => "企业微信",
        4 => "抖音",
        5 => "独立平台"
    );

    public $mark = array(
        1 => "wechat",
        2 => "app",
        3 => "work_wechat",
        4 => "douyin",
        5 => "free"
    );

    public $PlatformClass = array(
        1 => PlatformWechat::class,
        //        2 => PlatformWxapp::class,
        //        5 => false
    );

    public $type = array(
        'config' => 'json'
    );

    public function wechat()
    {
        return $this->hasOne(PlatformWechat::class, 'plaid');
    }

    public function wxapp()
    {
        return $this->hasOne(PlatformWxapp::class, 'plaid');
    }

    /**
     * 获取平台内容
     * @param $plaid
     * @return array|\think\Model|null
     */
    public function getPlatformByPlaid($plaid)
    {
        $res = $this->where(['plaid' => $plaid])->append(['details', 'type_name'])->find();
        return $res;
    }

    public function user()
    {
        return $this->hasOne(User::class, 'uid', 'uid');
    }

    //获取器

    public function getDetailsAttr($value, $data)
    {
        if (@$this->PlatformClass[$data['type']]) {
            $class = new $this->PlatformClass[$data['type']];
            return $class->getPlatformDetails($data['plaid']);
        } else {
            return false;
        }
    }

    public function getTypeNameAttr($value, $data)
    {
        return $this->_type[$data['type']];
    }

    public function getTypeMarkAttr($value, $data)
    {
        return $this->mark[$data['type']];
    }

    public function getAddonsAttr($value, $data)
    {
        $user = get_user();
        $where = [];
        $is_admin = $user['is_admin'] ?? 0;
        if (!$is_admin && $data['uid'] != $user['uid']) {
            $addon_names = (new UserPermission())->getAddonNames($user['uid'], $data['plaid']);
            $where = [['addon_name', 'in', $addon_names]];
        }
        if ($data['type'] === 2) {
            // 小程序单独判断
            $addons = (new PlatformApp())
                ->where(['plaid' => $data['plaid'], 'index' => 1])
                ->where($where)
                ->order('sort', 'asc')
                ->append(['addon'])
                ->withSearch(['addon'])
                ->hidden(['config', 'token','other_data'])
                ->select()->toArray();
            foreach ($addons as $key => $value) {
                if ($value['name']) {
                    $addons[$key]['addon']['title'] = $value['name'];
                }
                if ($value['logo']) {
                    $addons[$key]['addon']['icon'] = $value['logo'];
                }
            }
        } else {
            $addons = (new PlatformAddon())->where(['plaid' => $data['plaid'], 'index' => 1])->where($where)
                ->order('sort', 'asc')
                ->append(['addon'])->withSearch(['addon'])->hidden(['config', 'token','description'])
                ->select()->toArray();
        }
        return $addons;
    }

    public function getPlaids($uid = 0)
    {
        if (!$uid) {
            return [];
        }
        $platform = $this->where(['uid' => $uid])->select();
        $plaids = [];
        foreach ($platform as $key => $value) {
            $plaids[] = $value['plaid'];
        }
        return $plaids;
    }
}
